library ykfview;

import 'package:flutter/material.dart';

class YKKeepAliveWrapper extends StatefulWidget {

  final bool keepAlive;
  final Widget child;

  const YKKeepAliveWrapper({super.key, required this.child, this.keepAlive = true});

  @override
  State<StatefulWidget> createState() => _YKKeepAliveWrapperState();
}

class _YKKeepAliveWrapperState extends State<YKKeepAliveWrapper> with AutomaticKeepAliveClientMixin {

  @override
  Widget build(BuildContext context) => widget.child;

  @override
  void didUpdateWidget(covariant YKKeepAliveWrapper oldWidget) {
    if (oldWidget.keepAlive != widget.keepAlive) {
      // keepAlive 状态需要更新，实现在 AutomaticKeepAliveClientMixin 中
      updateKeepAlive();
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  bool get wantKeepAlive => widget.keepAlive;
}